package com.mobi.Survey;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mobi.Survey.controller.SurveyPojo;
import com.mobi.Survey.uicomponents.SurveyCellButton;
import com.mobi.Survey.uicomponents.SurveyCellImage;

public class AdapterSurveyListCell extends AbstractAdapterSurveyList implements
		View.OnClickListener
{

	private LayoutInflater mInflater;
	private SurveyListScreen surveyScreen;
	private List<SurveyPair> pairs = new ArrayList<AdapterSurveyListCell.SurveyPair>();

	public AdapterSurveyListCell(SurveyListScreen surveyScreen)
	{

		this.surveyScreen = surveyScreen;
		this.mInflater = LayoutInflater.from(surveyScreen);

	}

	private void setFourSurveys(List<SurveyPojo> surveys)
	{
		int size = surveys.size();
		pairs.clear();
		for (int i = 0; i < size;)
		{

			SurveyPair pair = new SurveyPair();
			if (i < size)
			{
				pair.survey1 = surveys.get(i);

			}

			if (++i < size)
			{
				pair.survey2 = surveys.get(i);

			}

			if (++i < size)
			{
				pair.survey3 = surveys.get(i);

			}

			if (++i < size)
			{
				pair.survey4 = surveys.get(i);

			}
			++i;
			pairs.add(pair);
		}
	}

	private void setThreeSurveys(List<SurveyPojo> surveys)
	{
		int size = surveys.size();
		pairs.clear();
		for (int i = 0; i < size;)
		{

			SurveyPair fourSurvey = new SurveyPair();
			if (i < size)
			{
				fourSurvey.survey1 = surveys.get(i);

			}

			if (++i < size)
			{
				fourSurvey.survey2 = surveys.get(i);

			}

			if (++i < size)
			{
				fourSurvey.survey3 = surveys.get(i);

			}

			++i;
			pairs.add(fourSurvey);
		}
	}

	private void setTwoSurveys(List<SurveyPojo> surveys)
	{
		int size = surveys.size();
		pairs.clear();
		for (int i = 0; i < size;)
		{

			SurveyPair fourSurvey = new SurveyPair();
			if (i < size)
			{
				fourSurvey.survey1 = surveys.get(i);

			}

			if (++i < size)
			{
				fourSurvey.survey2 = surveys.get(i);

			}

			++i;
			pairs.add(fourSurvey);
		}
	}

	@Override
	public void setData(List<SurveyPojo> surveys)
	{
		// TODO Auto-generated method stub
		super.setData(surveys);

		switch (getLayoutId())
		{
		case LayoutIds.SURVEY_ROW_SMALL_ICON_3:
			setFourSurveys(surveys);
			break;

		case LayoutIds.SURVEY_ROW_MEDIUM_ICON_4:
			setThreeSurveys(surveys);
			break;
		case LayoutIds.SURVEY_ROW_LARGE_ICON_5:
			setTwoSurveys(surveys);
			break;
		}

	}

	@Override
	public int getCount()
	{

		return pairs.size();

	}

	@Override
	public Object getItem(int arg0)
	{

		return null;
	}

	@Override
	public long getItemId(int arg0)
	{

		return 0;
	}

	class ViewHolder
	{

		SurveyCellButton btnPopupMenu;
		TextView tvSurvey;
		SurveyCellImage ivIcon;

		SurveyCellButton btnPopupMenu2;
		TextView tvSurvey2;
		SurveyCellImage ivIcon2;

		SurveyCellButton btnPopupMenu3;
		TextView tvSurvey3;
		SurveyCellImage ivIcon3;

		SurveyCellButton btnPopupMenu4;
		TextView tvSurvey4;
		SurveyCellImage ivIcon4;

		View cell1;
		View cell2;
		View cell3;
		View cell4;

	}

	private void initUiForLargeCells(ViewHolder holder, View convertView)
	{
		holder.btnPopupMenu = (SurveyCellButton) convertView
				.findViewById(R.id.btnPopup);

		holder.tvSurvey = (TextView) convertView.findViewById(R.id.tvSurvey);

		holder.ivIcon = (SurveyCellImage) convertView.findViewById(R.id.ivIcon);

		holder.btnPopupMenu2 = (SurveyCellButton) convertView
				.findViewById(R.id.btnPopup2);

		holder.tvSurvey2 = (TextView) convertView.findViewById(R.id.tvSurvey2);

		holder.ivIcon2 = (SurveyCellImage) convertView
				.findViewById(R.id.ivIcon2);

		holder.cell1 = convertView.findViewById(R.id.cell1);
		holder.cell2 = convertView.findViewById(R.id.cell2);

	}

	private void initUiForMediumCells(ViewHolder holder, View convertView)
	{

		initUiForLargeCells(holder, convertView);
		holder.btnPopupMenu3 = (SurveyCellButton) convertView
				.findViewById(R.id.btnPopup3);

		holder.tvSurvey3 = (TextView) convertView.findViewById(R.id.tvSurvey3);

		holder.ivIcon3 = (SurveyCellImage) convertView
				.findViewById(R.id.ivIcon3);

		holder.cell3 = convertView.findViewById(R.id.cell3);

	}

	private void initUiForSmallCells(ViewHolder holder, View convertView)
	{
		initUiForMediumCells(holder, convertView);

		holder.btnPopupMenu4 = (SurveyCellButton) convertView
				.findViewById(R.id.btnPopup4);

		holder.tvSurvey4 = (TextView) convertView.findViewById(R.id.tvSurvey4);

		holder.ivIcon4 = (SurveyCellImage) convertView
				.findViewById(R.id.ivIcon4);

		holder.cell4 = convertView.findViewById(R.id.cell4);
	}

	private void setValuesForSmallCells(ViewHolder holder, SurveyPair surveys)
	{
		setValuesForMediumCells(holder, surveys);
		// //////
		SurveyPojo survey = surveys.survey4;
		if (null != survey)
		{
			holder.cell4.setTag(survey);
			if (null != survey.getSubscription()
					&& !survey.getSubscription().isNull())
			{
				holder.btnPopupMenu4.setSurveyForButton(survey);
				holder.btnPopupMenu4.setOnClickListener(this);
			} else
			{
				holder.btnPopupMenu4.setVisibility(View.INVISIBLE);
			}
			holder.tvSurvey4.setText(survey.getSurveyDiscription());
			holder.cell4.setOnClickListener(this);
			if (survey.getBitmapIcon() != null)
			{
				try
				{
					holder.ivIcon4.setImageBitmap(survey.getBitmapIcon());
				} catch (OutOfMemoryError e)
				{
					System.gc();
					Log.e(this.getClass().getSimpleName(),
							"Out of memory due too   app icon image");
					holder.ivIcon.setImageDrawable(surveyScreen.getResources()
							.getDrawable(R.drawable.icon));

				}

			} else
			{
				holder.ivIcon4.setImageDrawable(surveyScreen.getResources()
						.getDrawable(R.drawable.icon));
			}

		} else
		{
			holder.cell4.setVisibility(View.INVISIBLE);
		}

	}

	private void setValuesForMediumCells(ViewHolder holder, SurveyPair surveys)
	{

		setValuesForLargeCells(holder, surveys);
		// ////////
		SurveyPojo survey = surveys.survey3;
		if (null != survey)
		{
			holder.cell3.setTag(survey);
			if (null != survey.getSubscription()
					&& !survey.getSubscription().isNull())
			{
				holder.btnPopupMenu3.setSurveyForButton(survey);
				holder.btnPopupMenu3.setOnClickListener(this);
			} else
			{
				holder.btnPopupMenu3.setVisibility(View.INVISIBLE);
			}
			holder.tvSurvey3.setText(survey.getSurveyDiscription());
			holder.cell3.setOnClickListener(this);
			if (survey.getBitmapIcon() != null)
			{
				try
				{
					holder.ivIcon3.setImageBitmap(survey.getBitmapIcon());
				} catch (OutOfMemoryError e)
				{
					System.gc();
					Log.e(this.getClass().getSimpleName(),
							"Out of memory due too   app icon image");
					holder.ivIcon.setImageDrawable(surveyScreen.getResources()
							.getDrawable(R.drawable.icon));

				}

			} else
			{
				holder.ivIcon3.setImageDrawable(surveyScreen.getResources()
						.getDrawable(R.drawable.icon));
			}

		} else
		{
			holder.cell3.setVisibility(View.INVISIBLE);
		}

	}

	private void setValuesForLargeCells(ViewHolder holder, SurveyPair surveys)
	{
		SurveyPojo survey = surveys.survey1;
		if (null != survey)
		{
			holder.cell1.setTag(survey);
			if (null != survey.getSubscription()
					&& !survey.getSubscription().isNull())
			{
				holder.btnPopupMenu.setSurveyForButton(survey);
				holder.btnPopupMenu.setOnClickListener(this);
			} else
			{
				holder.btnPopupMenu.setVisibility(View.INVISIBLE);
			}

			holder.tvSurvey.setText(survey.getSurveyDiscription());

			holder.cell1.setOnClickListener(this);
			if (survey.getBitmapIcon() != null)
			{

				try
				{
					holder.ivIcon.setImageBitmap(survey.getBitmapIcon());
				} catch (OutOfMemoryError e)
				{
					System.gc();
					Log.e(this.getClass().getSimpleName(),
							"Out of memory due too   app icon image");
					holder.ivIcon.setImageDrawable(surveyScreen.getResources()
							.getDrawable(R.drawable.icon));

				}

			} else
			{
				holder.ivIcon.setImageDrawable(surveyScreen.getResources()
						.getDrawable(R.drawable.icon));
			}

		} else
		{
			holder.cell1.setVisibility(View.INVISIBLE);
		}

		// /////////
		survey = surveys.survey2;
		if (null != survey)
		{
			holder.cell2.setTag(survey);
			if (null != survey.getSubscription()
					&& !survey.getSubscription().isNull())
			{
				holder.btnPopupMenu2.setSurveyForButton(survey);
				holder.btnPopupMenu2.setOnClickListener(this);
			} else
			{
				holder.btnPopupMenu2.setVisibility(View.INVISIBLE);
			}
			holder.tvSurvey2.setText(survey.getSurveyDiscription());
			holder.cell2.setOnClickListener(this);
			if (survey.getBitmapIcon() != null)
			{
				try
				{
					holder.ivIcon2.setImageBitmap(survey.getBitmapIcon());
				} catch (OutOfMemoryError e)
				{
					System.gc();
					Log.e(this.getClass().getSimpleName(),
							"Out of memory due too   app icon image");
					holder.ivIcon.setImageDrawable(surveyScreen.getResources()
							.getDrawable(R.drawable.icon));

				}

			} else
			{
				holder.ivIcon2.setImageDrawable(surveyScreen.getResources()
						.getDrawable(R.drawable.icon));
			}

		} else
		{
			holder.cell2.setVisibility(View.INVISIBLE);
		}

	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2)
	{

		final ViewHolder holder;
		if (convertView == null)
		{

			convertView = mInflater.inflate(getLayoutId(), null);

			holder = new ViewHolder();

			switch (getLayoutId())
			{
			case LayoutIds.SURVEY_ROW_SMALL_ICON_3:
				initUiForSmallCells(holder, convertView);
				break;

			case LayoutIds.SURVEY_ROW_MEDIUM_ICON_4:
				initUiForMediumCells(holder, convertView);
				break;
			case LayoutIds.SURVEY_ROW_LARGE_ICON_5:
				initUiForLargeCells(holder, convertView);
				break;
			}

			convertView.setTag(holder);

		} else
		{
			holder = (ViewHolder) convertView.getTag();

		}

		switch (getLayoutId())
		{
		case LayoutIds.SURVEY_ROW_SMALL_ICON_3:
			setValuesForSmallCells(holder, pairs.get(position));
			break;

		case LayoutIds.SURVEY_ROW_MEDIUM_ICON_4:
			setValuesForMediumCells(holder, pairs.get(position));
			break;
		case LayoutIds.SURVEY_ROW_LARGE_ICON_5:
			setValuesForLargeCells(holder, pairs.get(position));
			break;
		}

		return convertView;

	}

	class SurveyPair
	{
		SurveyPojo survey1;
		SurveyPojo survey2;
		SurveyPojo survey3;
		SurveyPojo survey4;

	}

	@Override
	public void onClick(View view)
	{
		if (view instanceof SurveyCellButton)
		{
			surveyScreen.registerForContextMenu(view);
			view.showContextMenu();
		} else if (null != view.getTag() && view.getTag() instanceof SurveyPojo)
		{
			surveyScreen.onClick(new SurveyClick(R.id.btnStartSurvey,
					((SurveyPojo) view.getTag())));
		}
	}

	@Override
	public void dispose()
	{
		// TODO Auto-generated method stub
		super.dispose();
		pairs.clear();
	}
}
